﻿using SuperSocket.SocketBase.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WPF_SocketServer_Demo.RequestInfo;

namespace WPF_SocketServer_Demo.ReceiveFilter
{
    public class MyReceiveFilter : ReceiveFilterHelper<DTRequestInfo>
    {


        /// <summary>
        /// 重写方法
        /// </summary>
        /// <param name="readBuffer">过滤之后的数据缓存</param>
        /// <param name="offset">数据起始位置</param>
        /// <param name="length">数据缓存长度</param>
        /// <returns></returns>
        protected override DTRequestInfo ProcessMatchedRequest(byte[] readBuffer, int offset, int length)
        {
            // 查找分隔符
            int separatorIndex = Array.IndexOf(readBuffer, (byte)0x00, offset, length);

            if (separatorIndex < 0)
            {
                // 如果没有找到分隔符，返回空的请求信息
                return NullRequestInfo;
            }

            // 假设 Key 的长度是从 offset 开始到分隔符前的位置
            string key = Encoding.ASCII.GetString(readBuffer, offset, separatorIndex - offset);



            // Body 从分隔符之后开始，到结束标记之前
            byte[] body = new byte[length - (separatorIndex - offset + 1) - 1]; // 减去分隔符和结束标记的长度
            Array.Copy(readBuffer, separatorIndex + 1, body, 0, body.Length);

            return new DTRequestInfo(key, body);
        }
    }
}
